package at.moop.up;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.net.ftp.FTPClient;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class PhotosphereSubmitter {
	
	private static Context ctx;
	private static File file;
    
    /**
     * Submit Photo
     * 
     * @return int status code (0 = fail, 1 = success)
     * @throws IOException  
     */
    public static int submit(Uri uri, Context c) throws IOException {
    	ctx = c;
    	String path = getRealPathFromURI(uri);
    	FTPClient ftp = new FTPClient();
    	
    	try {
    		
    		// Get credentials
    		FTPCredentials credentials = UpSoldier.getFtpCredentials(c);
    		ftp.connect(InetAddress.getByName(credentials.getServer()));
    		ftp.login(credentials.getUser(), credentials.getPassword());
    		//ftp.changeWorkingDirectory("spherepics");
    		
    		if (ftp.getReplyString().contains("250") || ftp.getReplyString().contains("230")) {
    	        ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
    	        ftp.enterLocalPassiveMode();
    	        
    	        file = new File(path);
    	        FileInputStream ifile = new FileInputStream(file);
    	        CountingInputStream cis = new CountingInputStream(ifile){
    	        	protected void beforeRead(int n){
    	        		try {
							super.beforeRead(n);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
    	        		Log.i("Submitter", "Uploading " + n + "/" + file.length());
    	        	}
    	        };
    	        
    	        boolean result = ftp.storeFile(file.getName(), cis);
    	        ftp.logout();
    	        ftp.disconnect();
    	        
    	        if(result){
    	        	return 1;
    	        }
    	        
    	        return 0;
    	    } else {
    	    	Log.i("PhotosphereSubmitter", ftp.getReplyString());
    	    	return -50;
    	    }
    		
    		
    	} catch (SocketException e) {
    	    Log.e("PhotosphereSubmitter", e.getStackTrace().toString());
    	    e.printStackTrace();
    	} catch (UnknownHostException e) {
    	    Log.e("PhotosphereSubmitter", e.getStackTrace().toString());
    	    e.printStackTrace();
    	} catch (IOException e) {
    	    Log.e("PhotosphereSubmitter", e.getStackTrace().toString());
    	    e.printStackTrace();
    	}
    	
    	return -100;

    }
    
    public static String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

}
